//
//  JWDrawView.m
//  课后作业—画板
//
//  Created by Jorn.Wu on 15/8/5.
//  Copyright (c) 2015年 Jorn.Wu. All rights reserved.
//

#import "JWDrawView.h"
#import "JWPath.h"
#import "MBProgressHUD+MJ.h"

#define JWColorWithRGB(r , g, b, a) [UIColor colorWithRed:(r) / 255.f green:(g) / 255.f blue:(b) / 255.f alpha:(a)]

@interface JWDrawView ()<UIAlertViewDelegate>

@property (nonatomic, strong) UIColor *mColor;
@property (nonatomic, assign) CGFloat mLineWidth;
@property (nonatomic, strong) UIImage *mImage;

@property (nonatomic, assign) CGContextRef ctx;
@property (nonatomic, assign) CGPoint startP;
@property (nonatomic, strong) JWPath *bPath;

@property (nonatomic, strong) NSMutableArray *pathAr;       //用于存储路径

@end

@implementation JWDrawView

- (instancetype)initWithFrame:(CGRect)frame{
    
    if (self = [super initWithFrame:frame]) {
        
        self.backgroundColor = JWColorWithRGB(255, 192, 203, 1);
        _mColor = [UIColor blackColor];
        _mLineWidth = 2;
        
    }
    return self;
}

- (NSMutableArray *)pathAr{
    
    if (!_pathAr) {
        _pathAr = [NSMutableArray array];
    }
    
    return _pathAr;
}

- (void)drawRect:(CGRect)rect {
    
    if (!_pathAr) {
        
        return;
    }
    
    for (JWPath *obj in _pathAr) {
        
        if ([obj isMemberOfClass:[UIImage class]]) {
            UIImage *image = (UIImage *)obj;
            [image drawInRect:self.bounds];
        }
        else {
            JWPath *path = (JWPath *)obj;
            [path.color set];
            [path stroke];
        }
        
    }
 
}

- (CGPoint)pointWithTouches:(NSSet *)touches{
    UITouch *touch = [touches anyObject];
    CGPoint pos = [touch locationInView:self];
    return pos;
}

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{

    _startP = [self  pointWithTouches:touches];
    _bPath = [JWPath pathWithColor:_mColor lineWidth:_mLineWidth startPoint:_startP];
    [self.pathAr addObject:_bPath];
    
}

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{
    
    CGPoint addP = [self pointWithTouches:touches];
    [_bPath addLineToPoint:addP];
    [self setNeedsDisplay];
}

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{
    CGPoint endP = [self pointWithTouches:touches];
    [_bPath addLineToPoint:endP];
    
    [self setNeedsDisplay];
}

- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event{
    CGPoint cancelP = [self pointWithTouches:touches];
    [_bPath addLineToPoint:cancelP];
    [self setNeedsDisplay];
}



///--------------------------外接口方法----------------------------------------

- (void)image:(UIImage *)image{
    
    [self.pathAr addObject:image];      //当作一个”path“ 放到数组里区，画的时候再判断 ,一定用self.pathAr，先调用set方法；
    [self setNeedsDisplay];
    
}
- (void)color:(UIColor *)color{
    self.mColor = color;
}

- (void)lineWidth:(CGFloat)width{
    self.mLineWidth = width;
}

- (void)eraser{
    self.mColor = self.backgroundColor;
}

- (void)undo{
    [_pathAr removeLastObject];
    [self setNeedsDisplay];
}

- (void)clear{
    [_pathAr removeAllObjects];
    [self setNeedsDisplay];
}

- (void)save{
//    截屏
    
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"确定要保存图片" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
    [alert show];
    
}

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
    if (buttonIndex == 1) {
        UIGraphicsBeginImageContext(self.bounds.size);
        
        CGContextRef context = UIGraphicsGetCurrentContext();
        [self.layer renderInContext:context];
        UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();
        
        UIImageWriteToSavedPhotosAlbum(image, self, @selector(image:didFinishSavingWithError:contextInfo:), nil);
    }
}

- (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo{
    if (error) {//保存失败
        [MBProgressHUD showError:@"保存失败！"];
    }else {//成功
        [MBProgressHUD showSuccess:@"保存成功！"];
    }
}

@end
